import deviceApi from '@/api/device/device'

export default {
  name: 'device',
  data() {
    return {
      formVisible: false,
      formTitle: '添加设备',

      listQuery: {
        current: 1,
        size: 20,
        beginTime:undefined,
        endTime:undefined,
        criteria:{
            deviceName: undefined,
            deviceNum: undefined,
            warehouseId: '',
            deviceType: '',
        }
      },
      listLifeQuery:{
        current: 1,
        size: 20,
        criteria:{
          deviceId: '',
          status: ''
        }
      },
      form : {
        deviceName: '',
        deviceNum: '',
        id: '',
        remark: '',
        deviceType: '',
        warehouseId: '',
        upperThreshold:'',
            lowerThreshold:'',
        num:0
      },
      unit:'',
      rules: {
        deviceNum: [
          {required: true, message: '请输入设备编码', trigger: 'blur'},
          {min: 3, max: 56, message: '长度在 3 到 56 个字符', trigger: 'blur'}
        ],
        deviceName: [
          {required: true, message: '请输入设备名称', trigger: 'blur'},
          {min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur'}
        ],
        deviceType: [
            {required: true, message: '请选择设备类型', trigger: 'blur'},
          ],
          warehouseId: [
            {required: true, message: '请选择所属仓库', trigger: 'blur'},
          ]
      },
      total: 0,
      list: null,
      totalLife: 0,
      listLife: null,
      listLoading: true,
      listLifeLoading: true,
      selRow: {},
      dictTypeList:[],
      storeHouseList:[],
      isAdd:true,
      recordVisible:false,
    }
  },
  filters: {
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.fetchData()
      this.getDictType()
      this.getStoreHouse()
    },
     fetchData() {
      this.listLoading = true
      deviceApi.getList(this.listQuery).then(response => {
              this.list = response.data.records
              this.listLoading = false
              this.total = response.data.total
            })
  
    },
    search() {
      this.listQuery.current = 1
      this.fetchData()
    },
    reset() {
      this.listQuery.criteria.deviceName = ''
      this.listQuery.criteria.warehouseId = ''
      this.listQuery.criteria.deviceType = ''
      this.listQuery.current = 1
      this.listQuery.criteria.deviceNum = ''
      this.fetchData()
    },
    handleFilter() {
      this.getList()
    },
    fetchNext() {
      this.listQuery.current = this.listQuery.current + 1
      this.fetchData()
    },
    fetchLifeNext() {
      this.listLifeQuery.current = this.listLifeQuery.current + 1
      this.fetchDataLife()
    },
    fetchPrev() {
      this.listQuery.current = this.listQuery.current - 1
      this.fetchData()
    },
    fetchLifePrev() {
      this.listLifeQuery.current = this.listLifeQuery.current - 1
      this.fetchDataLife()
    },
    fetchcurrent(current) {
      this.listQuery.current = current
      this.fetchData()
    },
    fetchLifecurrent(current) {
      this.listLifeQuery.current = current
      this.fetchDataLife()
    },
    changeSize(size) {
      this.listQuery.size = size
      this.fetchData()
    },
    changeLifeSize(size) {
      this.listLifeQuery.size = size
      this.fetchDataLife()
    },
    handleCurrentChange(currentRow, oldCurrentRow) {
      this.selRow = currentRow
      this.unit = currentRow.unitName
    },
    resetForm() {
        this.form = {
            deviceNum: '',
            deviceNum: '',
            id: '',
            remark: '',
            deviceType: '',
            warehouseId: '',
            upperThreshold:'',
            lowerThreshold:'',
            num:this.total+1
          }
      
    },
    getDictType(){
        deviceApi.getType().then(response => {
            this.dictTypeList = response.data
        })
    },
    getStoreHouse(){
        deviceApi.getHouseList().then(response => {
            this.storeHouseList = response.data
        })
    },
    add() {
        this.resetForm()
        this.formTitle = '添加设备'
        this.formVisible = true
        this.isAdd = true
      },
      save(){
        this.$refs['form'].validate((valid) => {
                if (valid) {
                  if(this.form.upperThreshold=='' && this.form.lowerThreshold==''){
                    this.$message({
                      message: '请填写上限或者下限阈值',
                      type: 'warning'
                    })
                    return
                  }
                  if(this.isAdd){

                    deviceApi.save(this.form).then(response =>{
                      this.$message({
                        message: '提交成功',
                        type: 'success'
                      })
                      this.fetchData()
                      this.formVisible = false
                    })

                  }else{

                    deviceApi.update(this.form).then(response =>{
                      this.$message({
                        message: '提交成功',
                        type: 'success'
                      })
                      this.fetchData()
                      this.formVisible = false
                    })

                  }
                  
                } else {
                  console.log('error submit!!')
                  return false
                }
              })
      },
      fetchPage(page) {
        this.listQuery.current = page
        this.fetchDataLife()
      },
      fetchLifePage(page) {
        this.listLifeQuery.current = page
        this.fetchDataLife()
      },
      changeStatus(id){
        if(!id){
          return;
        }
        deviceApi.changeStatus(id).then(response => {
                this.$message({
                  message: '提交成功',
                  type: 'success'
                })
                this.fetchData()
              })
      },
      remove() {
            if (this.checkSel()) {
              const id = this.selRow.id
              this.$confirm('确定删除该记录?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {
                deviceApi.remove(id).then(response =>{
                  this.$message({
                    message: '提交成功',
                    type: 'success'
                  })
                  this.fetchData()
                })
              }).catch(() => {
              })
            }
          
          },
          checkSel() {
            if (this.selRow && this.selRow.id) {
              return true
            }
            this.$message({
              message: '请选中操作项',
              type: 'warning'
            })
            return false
          },
          removeItem(record) {
            this.selRow = record
            this.remove()
          },
          editItem(record) {
            this.selRow = Object.assign({}, record);
            this.edit()
          },
          edit() {
            if (this.checkSel()) {
              this.form = this.selRow
              this.formTitle = '修改设备'
              this.formVisible = true
              this.isAdd = false
            }
          },
          changeValue(value){
            const obj = this.dictTypeList.find(item => item.code === value);
            this.unit = obj.unit
          },
          openLife(id){
            this.listLifeQuery.criteria.deviceId = id
            this.listLifeQuery.beginTime = undefined
            this.listLifeQuery.endTime = undefined
            this.recordVisible = true
            this.fetchDataLife()
          },
          fetchDataLife() {
            this.listLifeLoading = true
            deviceApi.getLifeList(this.listLifeQuery).then(response => {
                    this.listLife = response.data.records
                    this.listLifeLoading = false
                    this.totalLife = response.data.total
                  })
        
          },
          searchLife() {
            this.listLifeQuery.current = 1
            this.fetchDataLife()
          },
          resetLife() {
            this.listLifeQuery.beginTime = undefined
            this.listLifeQuery.endTime = undefined
            this.listLifeQuery.criteria.status = ''
            this.listLifeQuery.current = 1
            this.fetchDataLife()
          },
          formatDate(originVal) {
              const dt = new Date(originVal);
              const y = dt.getFullYear();
              const m = (dt.getMonth() + 1 + "").padStart(2, "0");
              const d = (dt.getDate() + "").padStart(2, "0");
              const hh = (dt.getHours() + "").padStart(2, "0");
              const mm = (dt.getMinutes() + "").padStart(2, "0");
              const ss = (dt.getSeconds() + "").padStart(2, "0");
              return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
          },
          clearlife(){
            const id = this.listLifeQuery.criteria.deviceId
            if(id){
              this.$confirm('确定清空全部设备记录?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {
                deviceApi.clearLife(id).then(response => {
                  this.$message({
                    message: '清空成功',
                    type: 'sucess'
                  })
                  this.fetchDataLife()
                })
              })
            }
          }
            

  }
}